var _uri = 'platform_and_operated_stores_inquiry';
var _uri_query = 'platform_and_operated_stores';
$(function() {
	initDP();
	initTitles();
	//initChkbox();
});
function initDP(){
	var date_type = getRadioVal('time');
	var range = getDateRange(new Date(),date_type,'-');
	$(".dpicker").datepicker({onSelect:doOnSelect,changeMonth: true,changeYear: true,maxDate:0,minDate:-10*365,onChangeMonthYear:changeMonthYear,beforeShow:doBeforeShow,dateFormat: 'yy/mm/dd', showOn: 'both',  buttonImageOnly: true, buttonImage: 'images/calendar.png'}).val(range);
	$('input[name=time]').eq(0)[0].checked=true;
	$('input[name=time]').change(function(){
		var date_type = getRadioVal('time');
		var range = getDateRange(new Date(), date_type, '-');
		$(".dpicker").focus(function(){
			var type = getRadioVal('time');
			if(type=='year')
				$("#ui-datepicker-div table, select.ui-datepicker-month").hide();
			else if(type=='month')
				$("#ui-datepicker-div table").hide();
		}).val(range);
	});
	function doBeforeShow(input, inst){
		var type = getRadioVal('time');
		var val = $('#'+inst.id).val();
		$(this).datepicker('option', 'stepMonths', type=='year'?12:1);
		$(this).datepicker('option', 'showButtonPanel', type=='month'||type=='year');
		var height = $(input).height();
		var offset = $(input).offset();
		window.setTimeout(function(){
	        inst.dpDiv.css({top:(offset.top+height+4)+'px',left: offset.left+'px'});
	        $('#'+inst.id).val(val);
	    },1);
	}
	function changeMonthYear(year, month, inst){
		var type = getRadioVal('time');
		if(type=='month' || type=='year'){
			var range = getDateRange(new Date(year,month-1,1), type, '-');
			$('#'+inst.id).val(range);
		}
	}
	function doOnSelect(dateText, inst){
		var date_type = getRadioVal('time');
		var date = $(this).datepicker('getDate');
		var range = getDateRange(date, date_type, '-');
		$('#'+inst.id).val(range);
	}
}


function initTitles(){
	postHandler(_uri, {fun:'titles'}, function(val){
		initPopup(val);
		initPopupWindow();
		setTimeout('initChkbox()',500);
	});
}
var popup_counter = 0;
function initPopup(val){
	shop_counter = 0;
	var oWin = $('.checkbox_box_w');
	var html = '<label><input name="checkbox" type="checkbox" id="" checked="checked" value=""><strong style="color:#00aacc;">全部</strong></label><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk()" type="button" value="确定" />';
	oWin.html(html);
	var div = $('.checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	for(var i=0;i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var tok4 = tok3[0].split('_##_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked" value="'+(tok4.length==1?tok4[0]:'')+'"><strong>'+t1+'</strong></label>';
		if(tok2.length>1 && tok4.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var tok5 = tok3[j].split('_##_');
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked" value="'+tok5[1]+'">'+tok5[0]+'</label></div>';
				idx++;
				popup_counter ++;
			}
			_html += '</div>';
		}else
			popup_counter ++;
		_div.html(_html).appendTo(div);
	}
}
function chkboxOk(){
	var oTar = $('.checkbox_box_w');
	var clone = oTar.clone();
	var oChks = clone.find('input:checkbox');
	var str='';
	$('.show_select').html('<input type="hidden" id="chks_data" value=""/></div>');
	oTar.find('input:checkbox').each(function(idx){
		var chk = $(this)[0].checked;
		var oDiv = oChks.eq(idx).closest('div');
		if(oDiv.parent().hasClass('checkbox_box_w')) oDiv = oDiv.parent();
		if(chk==false){
			oDiv.remove();
		}else{
			if($(this).val().length>0)
			str += ((str.length>0?',':'')+$(this).val());
			oChks.eq(idx).remove();
		}
		if(idx=oChks.length-1){
			str = str.replace(/,/g,'_###_');
			$('.show_select').html('<div class="checkbox_box_w">'+clone.find('.checkbox_box:eq(0)').html()+'<input type="hidden" id="chks_data" value="'+str+'"/></div>');
			clone.remove();
		}
	});
	
}
function query(){
	var datas = {fun:'query',date_type:getRadioVal('time')};
	var oDate = $('#date_range');
	oDate.val($.trim(oDate.val()));
	var datas = {fun:'query',date_type:getRadioVal('time'),date_range:oDate.val()};
	var tok = oDate.val().split(' ~ ');
	if(oDate.val()==''){
		alert('请输入期间');oDate.focus();return;
	}else if((tok.length!=2 || !isDateStr(tok[0]) || !isDateStr(tok[1]))){
		alert('日期有误');oDate.focus();return;
	}else if(compareDateStr(tok[0], tok[1])<0){
		alert('结束日期应大于开始日期');oDate.focus();return;
	}
	datas['begin_time'] = tok[0];
	datas['end_time'] = tok[1];
	var date = str2date(tok[0]);
	date.setDate(date.getDate()-1);
	var prevRange = getDateRange(date, 'week', '-');
	var prevTok = prevRange.split(' ~ ');
	datas['prev_begin_time'] = prevTok[0];
	datas['prev_end_time'] = prevTok[1];
	
	if($('#chks_data').val()!=undefined){
		var chks_data = $('#chks_data').val();
		var counter = chks_data.split('_###_').length;
		if(chks_data.length>0) datas['shopid'] = chks_data;
	}
	postHandler(_uri,datas, function(val){
		window.location=_uri_query+'.html';
		return false;
	});
}